# -*- coding: utf-8 -*-
"""
Created on Tue Aug 21 11:07:35 2018

@author: Who

31区
http://www.31qu.cn/

"""

import requests
import re
import time
from cs import day
from WriteData import writedata
from Tk import genearteMD5


def post_html_text(url, data):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    try:
        r = requests.post(url, timeout=30, data=data, headers=header)
        r.raise_for_status()  # 如果状态不是200, 引发HTTPError异常
        r.encoding = r.apparent_encoding
        html = r.text
        return html

    except:
        return None


def get_dates_Kx(url, data):
    text = post_html_text(url, data)
    if not text:
        return None
    regex = re.compile(r'<div class="pub_time" data-time="([\s\S]*?)000">')
    datas = re.findall(regex, text)
    dates = [time.strftime("%Y-%m-%d %X", time.localtime(int(date)))[:10] for date in datas]
    return dates


def get_dates_Xw(url, data):
    text = post_html_text(url, data)
    if not text:
        return None
    regex = re.compile(r'<span class="time pub_time fl" data-time="([\s\S]*?)000">')
    datas = re.findall(regex, text)
    dates = [time.strftime("%Y-%m-%d %X", time.localtime(int(date)))[:10] for date in datas]
    return dates


def count_process_one():  # 快讯
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:

        data = {
            'page_no': a,
            'page_size': '20',
            'search': ''
        }
        url = 'http://www.31qu.cn/news/news_list?channel=flash_news&type=1'
        dates = get_dates_Kx(url, data)
        if not dates:
            break
        print('---- 31区快讯获取第{}页----'.format(a))
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def count_process_two():  # 新闻
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        data = {
            'page_no': a,
            'page_size': '15',

        }
        url = 'http://www.31qu.cn/news/news_list?channel=news&type=0'
        dates = get_dates_Xw(url, data)
        print('---- 31区新闻获取第{}页----'.format(a))
        if not dates:
            break
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '31区'
    t1 = count_process_one()
    t2 = count_process_two()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
